package com.sls.api.game.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sls.api.game.pojo.OhFeiOrderStatusInfo;
import com.sls.api.util.IpUtil;
import com.sls.api.util.ResponseDataXmlFormat;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;

/**
 * 
 * @description 殴飞订单处理结果通知<br>
 *              post方式访问
 * @author Wangxuegang
 * @date 2012-12-12
 */
public class OhFeiOrderStatusServlet extends SuperServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(OhFeiOrderStatusServlet.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setAttribute("org.eclipse.jetty.server.Request.queryEncoding",
				"UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		resp.setStatus(HttpServletResponse.SC_OK);

		// 充值后状态
		String ret_code = req.getParameter("ret_code");
		// sls订单号
		String sporder_id = req.getParameter("sporder_id");
		// 成功处理时间
		String ordersuccesstime = req.getParameter("ordersuccesstime");
		// 失败原因
		String err_msg = req.getParameter("err_msg");
		
		log.info("殴飞回调推送订单处理结果：" + "\n订单最终处理结果：" + ret_code
				+ "\n手拉手订单号：" + sporder_id + "\n订单处理时间：" + ordersuccesstime
				+ "失败原因：" + err_msg);
		
		boolean flag = checkData(ret_code, sporder_id, resp);
		if (flag) {
			try {
				Gson gson = CommonUtil.gson();
				SocketDTO socketChangeVo = createMsg(ret_code, sporder_id,
						ordersuccesstime, err_msg, req);
				Head head = socketChangeVo.getHead();
				String resCode = head.getResponseCode();
				String resMsg = head.getResponseMessage();
				resMsg = URLEncoder.encode(resMsg, "GBK"); // GBK编码

				String bodyStr = socketChangeVo.getBody();
				String resBodyStr = gson.fromJson(bodyStr, String.class);
				String xmlStr = ResponseDataXmlFormat.assembleXmlStrForOrder(
						resBodyStr, resCode, resMsg, sporder_id);
				resp.getWriter().write(xmlStr);
				resp.getWriter().flush();
				resp.getWriter().close();
			} catch (Exception e) {
				log.error("出错", e);
				ResponseDataXmlFormat.returnResMsg("201", "其他错误", resp);
			}
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	/**
	 * 向service层发送请求消息
	 * 
	 * @param ret_code
	 * @param sporder_id
	 * @param ordersuccesstime
	 * @param err_msg
	 * @param req
	 * @return
	 */
	private SocketDTO createMsg(String ret_code, String sporder_id,
			String ordersuccesstime, String err_msg, HttpServletRequest req) {
		OhFeiOrderStatusInfo ohFeiInfo = new OhFeiOrderStatusInfo();
		ohFeiInfo.setRet_code(ret_code);
		ohFeiInfo.setSporder_id(sporder_id);
		ohFeiInfo.setOrdersuccesstime(ordersuccesstime);
		ohFeiInfo.setErr_msg(err_msg);

		SocketDTO socketChangeVo = new SocketDTO();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020903");
		head.setIp(IpUtil.getIpAddr(req));
		head.setUserId("0"); // 
		socketChangeVo.setHead(head); // 报文头部
		return super.send(head, ohFeiInfo);
	}

	/**
	 * 
	 * @param ret_code
	 * @param sporder_id
	 * @param ordersuccesstime
	 * @param err_msg
	 * @return
	 * @throws IOException
	 */
	private boolean checkData(String ret_code, String sporder_id,
			HttpServletResponse resp) throws IOException {
		boolean flag = false;
		if ("".equals(ret_code) || ret_code == null) {
			ResponseDataXmlFormat.returnResMsg("101", "缺少必需参数[ret_code]", resp);
		} else if ("".equals(sporder_id) || sporder_id == null) {
			ResponseDataXmlFormat.returnResMsg("101", "缺少必需参数[sporder_id]",
					resp);
		} else {
			flag = true;
		}

		return flag;
	}

}
